package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public void importOrderSettings(List<String[]> list) throws Exception {
        List<OrderSetting> saveList = new ArrayList<>();
        List<OrderSetting> updateList = new ArrayList<>();
        List<OrderSetting> orderSettingList = getOrderSettingList(list);
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
            QueryWrapper queryWrapper = new QueryWrapper();
            String s = DateUtils.parseDate2String(orderDate,"yyyy-MM-dd");
            queryWrapper.eq("orderdate",s);
            OrderSetting orderSettingIs = baseMapper.selectOne(queryWrapper);
            if(orderSettingIs==null){
                saveList.add(orderSetting);
            }else {
                orderSetting.setId(orderSettingIs.getId());
                updateList.add(orderSetting);
            }
        }
        saveBatch(saveList);
        updateBatchById(updateList);
    }

    @Override
    public Map findSettingDate(int year, int month) {
        String begin = year+"-"+month+"-1";
        String end = year+"-"+month+"-31";
        System.out.println(begin+"===="+end);
        List<OrderSetting> list = baseMapper.findSettingDate(begin,end);
        Map map = new HashMap();
        for (OrderSetting orderSetting : list) {
            String s = DateUtils.parseDate2String(orderSetting.getOrderDate(), "yyyy-MM-dd");
            OrderSetting orderSetting1 = new OrderSetting();
            orderSetting1.setNumber(orderSetting.getNumber());
            orderSetting1.setReservations(orderSetting.getReservations());
            map.put(s,orderSetting1);
        }
        System.out.println(map);
        return map;
    }

    @Override
    public void updateSettingDate(String s, int number) {
        baseMapper.updateSettingDate(s,number);
    }

    @Override
    public int checkOrderSetting(String orderDate) {
        return baseMapper.checkOrderSetting(orderDate);
    }

    @Override
    public void updateReservations(String orderDate) {
        baseMapper.updateReservations(orderDate);
    }

    private List<OrderSetting> getOrderSettingList(List<String[]> list) {
        if (list!=null) {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : list) {
                OrderSetting orderSetting = new OrderSetting();
                Date date = DateUtils.parseString2Date(strings[0],"yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSetting.setReservations(0);
                orderSettingList.add(orderSetting);
            }
            return orderSettingList;
        }else {
            return null;
        }
    }
}
